package com.qidian.zl.paper.dao.impl;


import com.qidian.zl.paper.dao.AdminDAO;
import com.qidian.zl.paper.dao.BaseDAO;
import com.qidian.zl.paper.pojo.Admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AdminDAOImpl extends BaseDAO implements AdminDAO {
    @Override
    public int queryTotal(Admin admin) {
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        int total = 0;
        String sql = "select count(*) from admin where 1 = 1";
        if(admin != null){
            if(admin.getAdminName()!=null && !"".equals(admin.getAdminName())){
                sql += " and admin_name like '%"+admin.getAdminName()+"%'";
            }
            if(admin.getAdminStatus()!=null){
                sql += " and admin_status = "+admin.getAdminStatus();
            }
        }
        try {
            con = getCon();
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            if(rs.next()) {
                total = rs.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(rs,pst,con);
        }
        return total;
    }

    @Override
    public List<Admin> queryByPage(int page, int size, Admin admin) {
        List<Admin> admins = new ArrayList<>();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "select * from admin where 1 = 1";
        if(admin != null){
            if(admin.getAdminName()!=null && !"".equals(admin.getAdminName())){
                sql += " and admin_name like '%"+admin.getAdminName()+"%'";
            }
            if(admin.getAdminStatus()!=null){
                sql += " and admin_status = "+admin.getAdminStatus();
            }
        }
        sql += " limit ?,?";
        try {
            con = getCon();
            pst = con.prepareStatement(sql);
            pst.setInt(1,(page-1)*size);
            pst.setInt(2,size);
            rs = pst.executeQuery();
            while(rs.next()){
                Admin a = new Admin();
                a.setAdminCreated(rs.getString("admin_created"));
                a.setAdminId(rs.getLong("admin_id"));
                a.setAdminName(rs.getString("admin_name"));;
                a.setAdminPass(rs.getString("admin_pass"));
                a.setAdminStatus(rs.getInt("admin_status"));
                admins.add(a);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(rs,pst,con);
        }
        return admins;
    }

    @Override
    public int save(Admin admin) {
        int result = 0;
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "insert into admin (admin_name,admin_pass,admin_created,admin_status) " +
                " values(?,?,?,?)";
        try {
            con = getCon();
            pst = con.prepareStatement(sql);
            pst.setString(1,admin.getAdminName());
            pst.setString(2,admin.getAdminPass());
            pst.setString(3,admin.getAdminCreated());
            pst.setInt(4,admin.getAdminStatus());
            result = pst.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(rs,pst,con);
        }
        return result;
    }

    @Override
    public Admin queryByName(String adminName) {
        Admin admin = null;
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "select * from admin where admin_name=?";
        try {
            con = getCon();
            pst = con.prepareStatement(sql);
            pst.setString(1,adminName);
            rs = pst.executeQuery();
            if(rs.next()){
                admin = new Admin();
                admin.setAdminCreated(rs.getString("admin_created"));
                admin.setAdminId(rs.getLong("admin_id"));
                admin.setAdminName(rs.getString("admin_name"));;
                admin.setAdminPass(rs.getString("admin_pass"));
                admin.setAdminStatus(rs.getInt("admin_status"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(rs,pst,con);
        }
        return admin;
    }

    /**
     *
     * @param adminId
     * @return
     */
    @Override
    public Admin queryById(long adminId){
        Admin admin = null;
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "select * from admin where admin_id = ?";
        try {
            con = getCon();
            pst = con.prepareStatement(sql);
            pst.setLong(1,adminId);
            rs = pst.executeQuery();
            if(rs.next()){
                admin = new Admin();
                admin.setAdminCreated(rs.getString("admin_created"));
                admin.setAdminId(rs.getLong("admin_id"));
                admin.setAdminName(rs.getString("admin_name"));;
                admin.setAdminPass(rs.getString("admin_pass"));
                admin.setAdminStatus(rs.getInt("admin_status"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(rs,pst,con);
        }
        return admin;
    };

    @Override
    public int update(Admin admin) {
        int result = 0;
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "update admin set admin_name=?,admin_pass=?,admin_status=? " +
                " where admin_id = ?";
        try {
            con = getCon();
            pst = con.prepareStatement(sql);
            pst.setString(1,admin.getAdminName());
            pst.setString(2,admin.getAdminPass());
            pst.setInt(3,admin.getAdminStatus());
            pst.setLong(4,admin.getAdminId());
            result = pst.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(rs,pst,con);
        }
        return result;
    }
}
